/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.ThreadsCPUInterface;
import br.gov.cemaden.library.model.smp.ExecHardware;
import br.gov.cemaden.library.model.smp.ThreadsCPU;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class ThreadsCPUDao extends GenericDao implements ThreadsCPUInterface {

    private Session session;

    public ThreadsCPUDao(Session session) {
        this.session = session;
    }

    public List<ThreadsCPU> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadsCPU> threadsCpu = null;
        try {
            Query query = session.getNamedQuery("ThreadsCPU.findAll");
            threadsCpu = (List<ThreadsCPU>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadsCPUFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadsCpu;
    }

    public ThreadsCPU findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ThreadsCPU threadsCpu = null;
        try {
            Query query = session.getNamedQuery("ThreadsCPU.findById").setParameter("id", id);
            threadsCpu = (ThreadsCPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadsCPUFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadsCpu;
    }

    public List<ThreadsCPU> findByTempoCPUThreadTotal(String tempoCPUThreadTotal) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadsCPU> threadsCpu = null;
        try {
            Query query = session.getNamedQuery("ThreadsCPU.findByTempoCPUThreadTotal").setParameter("tempoCPUThreadTotal", tempoCPUThreadTotal);
            threadsCpu = (List<ThreadsCPU>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadsCPUFacade.findByTempoCPUThreadTotal: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadsCpu;
    }

    public List<ThreadsCPU> findByTempoCPUThreadUsuario(String tempoCPUThreadUsuario) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadsCPU> threadsCpu = null;
        try {
            Query query = session.getNamedQuery("ThreadsCPU.findByTempoCPUThreadUsuario").setParameter("tempoCPUThreadUsuario", tempoCPUThreadUsuario);
            threadsCpu = (List<ThreadsCPU>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadsCPUFacade.findByTempoCPUThreadUsuario: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadsCpu;
    }

    public List<ThreadsCPU> findByTempoCPUkernelThread(String tempoCPUkernelThread) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadsCPU> threadsCpu = null;
        try {
            Query query = session.getNamedQuery("ThreadsCPU.findByTempoCPUkernelThread").setParameter("tempoCPUkernelThread", tempoCPUkernelThread);
            threadsCpu = (List<ThreadsCPU>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadsCPUFacade.findByTempoCPUkernelThread: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadsCpu;
    }

    public List<ThreadsCPU> findByExecHardware(ExecHardware execHardware) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadsCPU> threadsCpu = null;
        try {
            Query query = session.getNamedQuery("ThreadsCPU.findByExecHardware").setParameter("execHardware", execHardware);
            threadsCpu = (List<ThreadsCPU>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadsCPUFacade.findByExecHardware: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadsCpu;
    }

    public ThreadsCPU insert(ThreadsCPU threadsCpu) {
        try {
            saveOrUpdate(this.session, threadsCpu);
        } catch (Exception ex) {
            Logger.getLogger(ThreadsCPUDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return threadsCpu;
    }

    public ThreadsCPU update(ThreadsCPU threadsCpu) {
        try {
            saveOrUpdate(this.session, threadsCpu);
        } catch (Exception ex) {
            Logger.getLogger(ThreadsCPUDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return threadsCpu;
    }

    public void delete(ThreadsCPU threadsCpu) {
        try {
            remove(this.session, threadsCpu);
        } catch (Exception ex) {
            Logger.getLogger(ThreadsCPUDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
